﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Fitment
{
    
    public class Userinfo
    {
        private int id;
        private string account;
        private string pwd;
        private string name;
        private string sex;
        private int age;
        private string telphone;
        private string email;
        private string address;
        private DS data;
        public Userinfo()
        {
            data = new DS();
            id = 0;
            this.account = "";
            this.pwd = "";
            this.name = "";
            this.sex = "";
            this.age = 0;
            this.telphone = "";
            this.email = "";
            this.address = "";
        }
        public Userinfo(int id)
        {
            data = new DS();
            this.id = id;
            this.account = "";
            this.pwd = "";
            this.name = "";
            this.sex = "";
            this.age = 0;
            this.telphone = "";
            this.email = "";
            this.address = "";
        }
        public Userinfo(int id, string acc, string pwd, string name, string sex, int age, string telphone, string email, string address)
        {
            data = new DS();
            this.id = id;
            this.account = acc;
            this.pwd = pwd;
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.telphone = telphone;
            this.email = email;
            this.address = address;
        }
        /*把用户信息插入到数据库中,插入不成功返回0， 插入成功返回1，用户名重复返回2*/
        public int save()
        {
            string sql = "SELECT ACCOUNT FROM USERINFO WHERE ACCOUNT = '" + account + "'";
            string existacc = data.ExcuteLogin(sql);
            if (existacc.Equals(account))//用户名重复了,返回2
            {
                return 2;
            }
            sql = "INSERT INTO USERINFO(ACCOUNT, PASSWORD, NAME, SEX, AGE, TELPHONE, ADDRESS, EMAIL) "
                + "VALUES('" + account + "', '" + pwd + "', '" + name + "', '" + sex + "', '" + age + "', '" + telphone + "', '"
                + address + "', '" + email + "');";
            //把用户注册信息插入到数据库中,插入成功则返回1
            if (data.ExcuteSql(sql))
            {
                return 1;
            }
            else //插入不成功返回0
            { 
                return 0;
            }
        }
        /*删除用户信息*/
        public bool delete()
        {
            return true;
        }
        /*修改用户信息*/
        public bool modifyInfo(string acc, string name, string sex, int age, string telphone, string email, string address)
        {
            if (id == 0)
            {
                return false;
            }
            string sql = "UPDATE USERINFO SET NAME = '" + name + "', SEX = '" + sex + "', AGE = '"
                   + age + "', TELPHONE = '" + telphone + "', EMAIL = '" + email + "', ADDRESS = '" + address + "' WHERE USER_ID = '" + id + "'";
            if (data.ExcuteSql(sql))
                return true;
            else
                return false;
        }
        /*查询用户信息*/
        public void findById()
        {
            string sql = "SELECT ACCOUNT, NAME, SEX, AGE, TELPHONE, EMAIL, ADDRESS FROM USERINFO WHERE USER_ID = '"
                    + id + "'";
            DataTable dt = data.GetDataSource(sql, "info");
            account = dt.Rows[0][0].ToString();
            name = dt.Rows[0][1].ToString();
            sex = dt.Rows[0][2].ToString();
            telphone = dt.Rows[0][4].ToString();
            email = dt.Rows[0][5].ToString();
            address = dt.Rows[0][6].ToString();
            if (dt.Rows[0][3].ToString().Equals("0"))
            {
                age = 0;
            }
            else
            {
                age = Convert.ToInt32(dt.Rows[0][3].ToString());
            }
        }
        /*修改用户密码，修改不成功返回0， 修改成功返回1，输入的原密码错误返回2*/
        public int modifyPwd(string oldPwd, string newPwd)
        {
            string sql = "SELECT PASSWORD FROM USERINFO WHERE USER_ID = '" + id + "'";
            string pwd = data.ExcuteLogin(sql);
            if (oldPwd.Equals(pwd))//原密码正确
            {
                sql = "UPDATE USERINFO SET PASSWORD = '" + newPwd + "' WHERE USER_ID = '" + id + "'";
                if (data.ExcuteSql(sql))
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else //原密码不正确
            {
                return 2;
            }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Account
        {
            get { return account; }
            set { account = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        public string Telphone
        {
            get { return telphone; }
            set { telphone = value; }
        }
        public string Eemail
        {
            get { return email; }
            set { email = value; }
        }
        public string Address
        {
            get { return address; }
            set { address = value; }
        }
    }
}
